package com.ruoyi.ExpertReport.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.DegreePoints.domain.DegreePoints;
import com.ruoyi.DegreePoints.mapper.DegreePointsMapper;
import com.ruoyi.Evaluation.domain.Evaluation;
import com.ruoyi.Evaluation.mapper.EvaluationMapper;
import com.ruoyi.Evaluation.vo.EvaluationStat;
import com.ruoyi.ExpertReport.service.ReportService;
import com.ruoyi.Papers.mapper.PapersMapper;
import com.ruoyi.ResearchProjects.mapper.ResearchProjectsMapper;
import com.ruoyi.Students.mapper.StudentsMapper;
import com.ruoyi.tutors.mapper.TutorsMapper;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

@Service
public class ReportServiceImpl_v  {

    //日志打印
    private  static final Logger log = LoggerFactory.getLogger(ReportServiceImpl_v.class);

    @Autowired
    private DegreePointsMapper degreePointsMapper;

    @Autowired
    private EvaluationMapper evaluationMapper;

    @Autowired
    private TutorsMapper tutorsMapper;

    @Autowired
    private StudentsMapper studentsMapper;

    @Autowired
    private PapersMapper papersMapper;

    @Autowired
    private ResearchProjectsMapper researchProjectsMapper;


//    @Override
    public byte[] generateReport(Long pointId) {
        try{
            log.info("开始生成报告");

            //获取学位点的信息
            DegreePoints degreePoints = degreePointsMapper.selectDegreePointsByPointId(pointId);

            //获取评审信息
            Evaluation evaluations = evaluationMapper.selectByPointId(getUserId());
            //获取个评分统计信息
            List<EvaluationStat> countMap = evaluationMapper.selectCountEvaluation(getUserId());

            //基本信息
            XWPFDocument document = new XWPFDocument();

            //添加标题
            addTitle(document,"学位点评估报告");

            //基本信息
            addSection(document,"基本信息");
            addKeyValue(document,"学位点名称",degreePoints.getPointName());
            addKeyValue(document,"编码",degreePoints.getPointCode());
            addKeyValue(document,"所属院系",degreePoints.getDepartment());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("point_id",getUserId());
            addKeyValue(document,"导师数量",tutorsMapper.selectCount(queryWrapper).toString());
            addKeyValue(document,"学生数量",studentsMapper.selectCount(queryWrapper).toString());

            //科研情况
            addSection(document,"科研情况");
            addKeyValue(document,"论文数量",papersMapper.selectCount(queryWrapper).toString());
            addKeyValue(document,"项目数量",researchProjectsMapper.selectCount(queryWrapper).toString());
            addKeyValue(document,"科研经费",degreePointsMapper.getResearchFunding(getUserId()).toString());

            //评审结果
            addSection(document,"评审结果");
            addKeyValue(document,"学生信息评分:",evaluations.getStudentEvaluation());
            addKeyValue(document,"学生信息评语:",evaluations.getStudentComments());

            addKeyValue(document,"教师信息评分:",evaluations.getTutorsEvaluation());
            addKeyValue(document,"教师信息评语:",evaluations.getTutorsComments());

            addKeyValue(document,"课程信息评分:",evaluations.getCoursesEvalutaion());
            addKeyValue(document,"课程信息评语:",evaluations.getCoursesComments());

            addKeyValue(document,"科研信息评分:",evaluations.getResearchEvaluation());
            addKeyValue(document,"科研信息评语:",evaluations.getResearchComments());

            addKeyValue(document,"论文信息评分:",evaluations.getPapersEvaluation());
            addKeyValue(document,"论文信息评语:",evaluations.getPapersComments());

            addKeyValue(document,"获奖信息评分:",evaluations.getAwardsEvaluation());
            addKeyValue(document,"获奖信息评语:",evaluations.getAwardsComments());

            //计算总分
            Double count = (countMap.get(0).getTotalCount()) + (countMap.get(1).getTotalCount()*0.5) + (countMap.get(2).getTotalCount() * 0.25);
            addSection(document,"评分总和:" + count);

            //输出文档
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            document.write(byteArrayOutputStream);
            document.close();
            log.info("生成报告成功");
            return byteArrayOutputStream.toByteArray();
        }catch (Exception e){
            throw new RuntimeException("生成报告失败: " + e.getMessage());
        }
    }

    private void addTitle(XWPFDocument document, String title) {
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun run = paragraph.createRun();
        run.setText(title);
        run.setBold(true);
        run.setFontSize(16);
    }

    private void addSection(XWPFDocument document, String title) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(title);
        run.setBold(true);
        run.setFontSize(14);
    }

    private void addKeyValue(XWPFDocument document, String key, String value) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(key + "：" + value);
        run.setFontSize(12);
    }
}
